<?php
/**
 * 接口基础类
 * @author maniac
 */
namespace app\api\controller\v1;

use \think\Controller;
use app\facade\Aes;
use think\facade\Cache;
use app\common\model\Members;
use app\facade\Index;

use think\facade\Log As Log;

class Base extends Controller
{
	
	protected $m_info=[];//用户信息
    protected $role_id=0;//用户角色
	
    public function initialize()
    {

        $statistical = false;//记录接口访问
        if($statistical){
            $controller = request()->controller();
            $action     = request()->action();
            $ip         = request()->ip();
            $parameter  = request()->param();
            Index::statistical($controller,$action,$ip,$parameter);
        }


        if (!config('app_debug') && !array_key_exists('test',input('param.'))) {
        	$controller = request()->controller();
        	if($controller!='V1.upload' && request()->action()!='notify' && request()->action()!='refundnotify'){
        		$this->checkSign();
        	}
        }
    }
    
    /**
     * 校验 sign
     */
    public function checkSign()
    {
        $headers = request()->header();
        if (!isset($headers['signature']) || !trim($headers['signature'])) {
            exception('sign 校验失败，签名错误');
        }
        
        parse_str(Aes::decrypt($headers['signature']), $data);
        if (isset($data['aesKey'])) {
        	$data['aes_key'] = $data['aesKey'];
        }
        //log::write($data,'notice');
        if ($data['aes_key'] != md5(config('site.aes_key'))) {
            exception('sign 检验失败，签名错误');
        }
        if (time() - ceil($data['timestamp'] / 1000) > config('site.sign_expired_time')) {
            exception('sign 检验失败，签名已过期'.date("Y-m-d H:i:s"));
        }
        
        if (Cache::get($headers['signature'])) {
            exception('sign 检验失败，签名已失效');
        }
        Cache::set($headers['signature'], 1, config('site.sign_cache_time'));
    }
    
    /**
     * 校验 userToken
     */
    public function checkUserToken()
    {
        $userToken = input('userToken/s', '');
        if (!$userToken) {
            $ret = array(
                'status'=>-999,
                'message'=>'请先登录1',
                'data'=>$userToken
            );
            $result = json_encode($ret);
            exit(''.$result) ;
        }
		$userToken=preg_replace('/\s+/','+',$userToken);
        parse_str(Aes::decrypt($userToken), $data);
        //$user = WechatUsers::field('id,openid')->where('id', $data['id'])->find();
        if(!isset($data['id'])){
            $ret = array(
                'status'=>-999,
                'message'=>'请先登录2',
                'data'=>$data
            );
            $result = json_encode($ret);
            exit(''.$result) ;
        }else{
            $user =Members::field(true)->where('id',$data['id'])->find();
            if (!$user) {
                $ret = array(
                    'status'=>-999,
                    'message'=>'请先登录3',
                    'data'=>$user
                );
                $result = json_encode($ret);
                exit(''.$result) ;

            }
			
		    $this->m_info=$user;
            $this->role_id=$user['role_id'];

            return $user['id'];
        }

    }
    
    /**
     * 解析 userToken
     * @param string $userToken userToken
     */
    public function parseUserToken($userToken)
    {
        parse_str(Aes::decrypt($userToken), $data);
        if(!$data){
			$ret = array(
				'status'=>-999,
				'message'=>'请先登录',
				'data'=>[]
			);
			$result = json_encode($ret);
			exit(''.$result) ;
		}
        return $data['id'];
    }
}
